﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace Royaumes_Libres.Database
{
    /// <summary>
    /// 
    /// </summary>
    /// <remarks>Patrick Chabot</remarks>
    public class cKingdom
    {
        protected int pPkKingdom;
        protected string pName;
        protected int pFkUser;
        protected int pFkWorld;
        protected int pFkRace;
        protected int pPositionX;
        protected int pPositionY;
        protected cRessource pRessource;

        public int PosX { get { return this.pPositionX; } }

        public int PosY { get { return this.pPositionY; } }

        public int PkKingdom { get { return this.pPkKingdom; } }

        public cRessource Ressource { get { return this.pRessource; } }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        /// <remarks>Patrick Chabot</remarks>
        public override string ToString()
        {
            return this.pName;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PkUser"></param>
        /// <param name="PkWorld"></param>
        /// <remarks>Patrick Chabot</remarks>
        public cKingdom(int PkUser, int PkWorld)
        {
            SqlCommand cmd = cSQL.GetDbJeuCommand();

            try
            {
                cmd.Connection.Open();

                cmd.CommandText = "cde.spGetKingdom";
                cmd.Parameters.Add(cSQL.Param("@PkUser", PkUser, SqlDbType.Int));
                cmd.Parameters.Add(cSQL.Param("@PkWorld", PkWorld, SqlDbType.Int));

                SqlDataReader dr = cmd.ExecuteReader();
                dr.Read();

                this.pPkKingdom = dr.GetInt32(0);
                this.pName = dr.GetString(1);
                this.pFkUser = dr.GetInt32(2);
                this.pFkWorld = dr.GetInt32(3);
                this.pFkRace = dr.GetInt32(4);
                this.pPositionX = dr.GetInt32(5);
                this.pPositionY = dr.GetInt32(6);
                this.pRessource = cRessource.GetRessource(this.pPkKingdom);

                cmd.Connection.Close();
            }
            catch (Exception ex)
            {
                cmd.Connection.Close();
                throw new Exception("cKingdom(): " + ex.Message);
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="PkUser"></param>
        /// <param name="PkWorld"></param>
        /// <remarks>Francis Gagne</remarks>
        public cKingdom(int PkKingdom)
        {
            SqlCommand cmd = cSQL.GetDbJeuCommand();

            try
            {
                cmd.Connection.Open();

                cmd.CommandText = "cde.spGetKingdomFromPk";
                cmd.Parameters.Add(cSQL.Param("@PkKingdom", PkKingdom, SqlDbType.Int));

                SqlDataReader dr = cmd.ExecuteReader();
                dr.Read();

                this.pPkKingdom = dr.GetInt32(0);
                this.pName = dr.GetString(1);
                this.pFkUser = dr.GetInt32(2);
                this.pFkWorld = dr.GetInt32(3);
                this.pFkRace = dr.GetInt32(4);
                this.pPositionX = dr.GetInt32(5);
                this.pPositionY = dr.GetInt32(6);
                this.pRessource = cRessource.GetRessource(this.pPkKingdom);

                cmd.Connection.Close();
            }
            catch (Exception ex)
            {
                cmd.Connection.Close();
                throw new Exception("cKingdom(): " + ex.Message);
            }
        }

        public cRace GetRace()
        {
            return cRace.GetRace(this.pFkWorld, this.pFkRace);
        }

        public cWorks GetWorks()
        {
            return cWorks.GetWorks(this.pPkKingdom);
        }

        public cBuildings[] GetBuilding()
        {
            return cBuildings.GetBuildings(this.pPkKingdom);
        }

        public cOwnerBuildings[] GetOwnerBuilding()
        {
            return cOwnerBuildings.GetOwnerBuildings(this.pPkKingdom);
        }

        public cBuildingsBuilds GetBuildingsBuilds()
        {
            return cBuildingsBuilds.GetBuild(this.pPkKingdom);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="PkUser"></param>
        /// <param name="PkWorld"></param>
        /// <param name="PkRace"></param>
        /// <returns></returns>
        /// <remarks>Patrick Chabot</remarks>
        public static cKingdom NewKingdom(string Name, int PkUser, int PkWorld, int PkRace)
        {
            SqlCommand cmd = cSQL.GetDbJeuCommand();

            try
            {
                cmd.Connection.Open();

                cmd.CommandText = "cde.spNewKingdom";
                cmd.Parameters.Add(cSQL.Param("@Name", Name, SqlDbType.VarChar));
                cmd.Parameters.Add(cSQL.Param("@PkUser", PkUser, SqlDbType.Int));
                cmd.Parameters.Add(cSQL.Param("@PkWorld", PkWorld, SqlDbType.Int));
                cmd.Parameters.Add(cSQL.Param("@PkRace", PkRace, SqlDbType.Int));

                cmd.ExecuteNonQuery();
                cmd.Connection.Close();

                return new cKingdom(PkUser, PkWorld);
            }
            catch (Exception ex)
            {
                cmd.Connection.Close();
                throw new Exception("cKingdom.NewKingdom(): " + ex.Message);
            }
        }
    }
}